Tag: pthreads

C – 使用队列在线程之间传递消息的问题

我正在尝试使用队列在两个线程之间传递消息,但到目前为止我还没有得到任何结果。 当我在收到邮件之后和邮件发送之前打印邮件内容时,它似乎只能保持其价值。 我需要用1个服务器线程和多个客户端线程来实现它,但是现在我只使用1个。 这是我的代码 struct msg //struct for client requests to server { long mtype; int numResources; //number of resources to be requested int ID; //ID associated with client thread }; int c1PID; //process ID variable for client thread 1 int serverPID; key_t key1; key_t keyS; int msqid1; int msqidS; int main(int arc, char *argv[]) […]

将pthread作为输入并暂停的函数

我正在尝试从POSIX中的ExpressLogic移植实时Thread_Metric,以便为我的论文对Linux,Xenomai和RTAI的PREEMPT_RT补丁进行基准测试。 它们提供了具有以下function的C源文件,您必须实现这些function才能使基准测试工作: void tm_initialize(void (*test_initialization_function)(void)); int tm_thread_create(int thread_id, int priority, void (*entry_function)(void)); int tm_thread_resume(int thread_id); int tm_thread_suspend(int thread_id); void tm_thread_relinquish(void); void tm_thread_sleep(int seconds); int tm_queue_create(int queue_id); int tm_queue_send(int queue_id, unsigned long *message_ptr); int tm_queue_receive(int queue_id, unsigned long *message_ptr); int tm_semaphore_create(int semaphore_id); int tm_semaphore_get(int semaphore_id); int tm_semaphore_put(int semaphore_id); int tm_memory_pool_create(int pool_id); int tm_memory_pool_allocate(int pool_id, unsigned char […]

将堆栈变量传递给pthread_cleanup_push

我有一个使用文件描述符的线程,并且必须在取消时close()文件描述符。 伪代码看起来像: static void thread_cleanup(void *args) { int *fd = (int *)args; close(*fd); } void *thread(void *arg) { int fd = open(…); … pthread_cleanup_push(thread_cleanup, &fd); …. } 我有一个选择是将fd从thread传递给void * ,并让thread_cleanup将其thread_cleanup转换为int ,但如果sizeof(int) != sizeof(void *) ,这可能会导致问题。 我的问题是:当以这种方式使用时,正如在伪代码安全中那样传递堆栈变量吗?

单个生产者/消费者循环缓冲区,仅阻止消费者

我想用单个生产者和单个消费者实现一个缓冲区,其中只有消费者可能被阻止。 这里重要的细节是,如果队列已满,生产者可以删除更新。 我考虑过转换一个等待免费的实现,但乍一看似乎没有简单的方法来通知消费者新的数据已经到达而不会丢失通知。 所以我使用计数信号量确定了下面非常简单的方法(为清楚起见,省略了一些error handling细节): Object ar[SIZE]; int head = 0, tail = 0; sem_t semItems; // initialized to 0 void enqueue(Object o) { int val; sem_getvalue(&semItems, &val); if (val < SIZE – 1) { ar[head] = o; head = (head + 1) % SIZE; sem_post(&semItems); } else { // dropped } } Object dequeue(void) […]

线程参数的高效快捷方式

使用参数创建线程的最有效方法是什么? 参数是一个结构,如果结构不能保留在父线程堆栈上,则有两种解决方案。 具有动态内存分配 struct Arg{ int x; int y; }; void* my_thread(void* v_arg){ Arg* arg = (Arg*) v_arg; //… running delete arg; return NULL; } //Creating a thread void a_function(){ Arg* arg = new Arg; arg->x = 1; arg->y = 2; pthread_t t; pthread_create(&t, NULL, my_thread, arg); pthread_detach(t); } 用信号量 struct Arg{ sem_t sem; int […]

可能的堆栈损坏

参考我之前关于GDB未查明SIGSEGV点的问题 , 我的线程代码如下: void *runner(void *unused) { do { sem_wait(&x); … if(/*condition 1 check*/) { sem_post(&x); sleep(5); sem_wait(&x); if(/*repeat condition 1 check; after atleast 5 seconds*/) { printf(“LEAVING…\n”); sem_post(&x); // putting exit(0); here resolves the dilemma return(NULL); } } sem_post(&x); }while(1); } 主要代码: sem_t x; int main(void) { sem_init(&x,0,1); … pthread_t thrId; pthread_create(&thrId,NULL,runner,NULL); … pthread_join(thrId,NULL); […]

用信号中断c / c ++ readline

我试图用信号(SIGUSR1)中断readline,但显然如果没有处理信号,程序退出,处理时,readline继续进行,好像什么也没发生。 读取线应该能够使用信号中断。 我从另一个问题中得到了一个想法: 强制退出readline()函数 #include #include #include #include #include pthread_t main_id; void *thread_main(void* arg) { sleep(10); pthread_kill(main_id, SIGUSR1); } void signal_handler(int sig) { puts(“got signal”); (void) sig; } int main(int argc, char** argv) { struct sigaction sa; sa.sa_handler = signal_handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sigaction(SIGUSR1, &sa, NULL); main_id = pthread_self(); pthread_t id; pthread_create(&id, NULL, thread_main, […]

pthread_cleanup_push导致语法错误

我尝试在我的代码中添加一个部分,它可以在取消的情况下解锁互斥锁。 这可能会发生并导致死锁。 因此我尝试添加pthread_cleanup_push(cleanup_unlock_mutex, &mutex_ftdi); 但是这行会导致语法错误,我将其添加到代码文件末尾。 如果我注释代码行,程序将编译没有任何错误。 我做错了什么? void cleanup_unlock_mutex(void *p){ pthread_mutex_unlock(p); } ……. }else{ for(unsigned count=0; count <= number_of_requests; count++){ pthread_cleanup_push(cleanup_unlock_mutex, &mutex_ftdi); pthread_mutex_lock(&mutex_ftdi); process_requests(count, numberofthread); pthread_mutex_unlock(&mutex_ftdi); } } // compiler error: error: expected 'while' before '}' token ………. 文件中的所有其他函数都会收到警告:ISO C禁止嵌套函数[-pedantic]。

什么时候摧毁pthread屏障是否安全?

如果我有一个初始化的pthread_barrier_t,什么时候可以安全地销毁它? 以下示例是否安全? pthread_barrier_t barrier; … int rc = pthread_barrier_wait(b); if (rc != PTHREAD_BARRIER_SERIAL_THREAD && rc != 0){ perror(“pthread_barrier_wait”); exit(1); } if (id == 0){ if(pthread_barrier_destroy(&(threads[t_root].info.tmp_barrier))){ perror(“pthread_barrier_destroy”); exit(1); } }

POSIX线程退出/崩溃/exception崩溃,同时持有互斥锁

是否存在定义明确的POSIX互斥锁所有权行为 线程退出 线程崩溃 线程因exception而崩溃 假设thread-1拥有一个互斥锁。 并且线程2正在等待获取相同的互斥锁。 并且线程1进入1/2/3场景。 对thread-2有什么影响? PS:我认为自旋锁的行为是,不要解除阻塞线程2,并推断受自旋锁保护的部分无论如何都是坏的形状。