我正在使用沼澤標準 x86-64 Ubuntu gcc,顯然沒有編譯問題,因為我可以得到一個段錯誤。
我正在嘗試創建一個 pthread 來呼叫一個函式,該函式根據一些先前確定的條件采用另一個函式。也就是說,我試圖通過部分應用其輸入函式來修改執行緒中呼叫的函式。
代碼
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
void * (*foo)(void*);
void * (*bar(void (*outputFun)(uint8_t a, short unsigned int b)))(void *);
void baz(u_int8_t a, short unsigned int b) {
printf("a - b is %d - %d\n", a, b);
}
void bay(u_int8_t a, short unsigned int b) {
printf("b - a is %d - %d\n", b, a);
}
int main() {
srand(time(NULL));
unsigned int random = 1 rand() % 2;
printf("Our random number is: %d\n", random);
foo = bar(random > 1 ? *baz : *bay);
pthread_t thread;
pthread_create(&thread, NULL, foo , NULL);
// DO SOME STUFF
pthread_join(thread, NULL);
}
void * (*bar(void (*outputFun)(uint8_t a, short unsigned int b)))(void *) {
// DO OTHER STUFF
outputFun(1, 2);
return NULL;
}
valgrind 錯誤輸出
Our random number is: 2
a - b is 1 - 2
--15699-- REDIR: 0x4914b10 (libc.so.6:calloc) redirected to 0x483dce0 (calloc)
==15699== Thread 2:
==15699== Jump to the invalid address stated on the next line
==15699== at 0x0: ???
==15699== by 0x485E608: start_thread (pthread_create.c:477)
==15699== by 0x4998132: clone (clone.S:95)
==15699== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==15699==
==15699==
==15699== Process terminating with default action of signal 11 (SIGSEGV)
==15699== Bad permissions for mapped region at address 0x0
==15699== at 0x0: ???
==15699== by 0x485E608: start_thread (pthread_create.c:477)
==15699== by 0x4998132: clone (clone.S:95)
--15699-- REDIR: 0x49136d0 (libc.so.6:free) redirected to 0x483c9d0 (free)
==15699==
==15699== HEAP SUMMARY:
==15699== in use at exit: 272 bytes in 1 blocks
==15699== total heap usage: 2 allocs, 1 frees, 1,296 bytes allocated
==15699==
==15699== Searching for pointers to 1 not-freed blocks
==15699== Checked 8,476,712 bytes
==15699==
==15699== LEAK SUMMARY:
==15699== definitely lost: 0 bytes in 0 blocks
==15699== indirectly lost: 0 bytes in 0 blocks
==15699== possibly lost: 272 bytes in 1 blocks
==15699== still reachable: 0 bytes in 0 blocks
==15699== suppressed: 0 bytes in 0 blocks
==15699== Rerun with --leak-check=full to see details of leaked memory
==15699==
==15699== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==15699==
==15699== 1 errors in context 1 of 1:
==15699== Jump to the invalid address stated on the next line
==15699== at 0x0: ???
==15699== by 0x485E608: start_thread (pthread_create.c:477)
==15699== by 0x4998132: clone (clone.S:95)
==15699== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==15699==
==15699== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
Fwiw,我是一名 Java 開發人員。所以,我需要我能得到的所有幫助,我們將不勝感激。謝謝。
編輯 顯然,我不能讓 C 屈服于我的意愿。
uj5u.com熱心網友回復:
你可以做你想做的,只是不完全是你想要的。
一些問題:
- 隨著
foo = bar(random > 1 ? *baz : *bay);你試圖創建一個 C 沒有的“閉包” - 您不能使用(例如
*baz)呼叫函式。你需要baz() - 但是,我認為你只想要函式的地址(例如)
baz
對于執行緒,最后一個引數pthread_create是傳遞給執行緒函式的指標。您可以創建一個struct包含您需要的所有資訊/引數的檔案。您傳遞該結構的地址,執行緒函式可以訪問它。
這是一個以更標準的方式進行等效的重構:
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
typedef void (*outputFun) (uint8_t a, short unsigned int b);
typedef struct {
u_int8_t a;
unsigned int b;
outputFun foo;
} args_t;
void
baz(u_int8_t a, short unsigned int b)
{
printf("a - b is %d - %d\n", a, b);
}
void
bay(u_int8_t a, short unsigned int b)
{
printf("b - a is %d - %d\n", b, a);
}
void *
bar(void *vp)
{
args_t *arg = vp;
// DO OTHER STUFF
arg->foo(arg->a,arg->b);
return NULL;
}
int
main(void)
{
srand(time(NULL));
unsigned int random = 1 rand() % 2;
printf("Our random number is: %d\n", random);
args_t arg;
arg.foo = (random > 1) ? baz : bay;
arg.a = 23;
arg.b = 37;
pthread_t thread;
pthread_create(&thread, NULL, bar, &arg);
// DO SOME STUFF
pthread_join(thread, NULL);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/487797.html
下一篇:禁止未使用的靜態函式的錯誤訊息?
