Tag: multithreading

在execvp返回之后,为什么我的程序没有从它停止的地方开始?

我有一个像这样的代码块作为子线程运行: if(someVar == 1){ doSomeStuff; _exit(0) } else execvp(*(temp->_arguments), temp->_arguments); printf(“I’m done\n”); 当我用someVar == 1运行程序时,我知道_exit(0)调用会杀死我的线程。 但是,当它设置为0时,为什么程序在execvp()调用之后不继续并执行printf语句?

这个生产者 – 消费者实施中是否存在竞争条件?

在操作系统概念的第3.4.1节(Silberschatz,第9版)中,作者介绍了生产者 – 消费者问题,并给出了使用循环缓冲区的以下实现(第125,126页)。 //Shared variables #define BUFFER SIZE 10 struct Item; Item buffer[BUFFER SIZE]; int in = 0, out = 0; //buffer is empty when in == out //buffer is full when (in + 1) % BUFFER SIZE == out //Producer while (true) { Item next_produced = /*produce item here*/; while (((in + 1) % […]

为什么没有lpthread标志的gcc链接?

我正在做一个互动项目,其中互斥体表现得很神秘。 我把它归结为这个应该明显陷入僵局的测试用例。 #include #include int main() { pthread_mutex_t test; pthread_mutex_init(&test, NULL); pthread_mutex_lock(&test); pthread_mutex_lock(&test); printf(“Took lock twice\n”); return 0; } 但是,当我在没有-lpthread标志的情况下编译时,不仅程序仍然可以编译和链接,它也可以在没有死锁的情况下运行。 为什么? gcc pthread_break.c -o pthread_test ./pthread_test Took lock twice 使用-lpthread标志进行编译会产生预期结果: gcc pthread_break.c -o pthread_test -lpthread ./pthread_test <- deadlocked here 我正在运行GCC 7.2.0版。

完成后关闭线程

你什么时候关闭线程怎么样? 比如确保什么都没有打开或跑步? 到目前为止我知道如何打开它,但是……不是如何正确地关闭它 int iret1; pthread_t thread1; char *message1; void *multithreading1( void *ptr ) { while (1) { // Our function here } } int main (int argc, char * const argv[]) { if( (iret1=pthread_create( &thread1, NULL, multithreading1, (void*) message1)) ) { printf(“Thread creation failed: %d\n”, iret1); } return 0; }

从另一个线程调用CFRunLoopStop是否安全?

我的(主要是POSIX)应用程序的Mac构建产生一个子线程,它调用CFRunLoopRun()来执行事件循环(从MacOS获取网络配置更改事件)。 什么时候打包并离开,主线程在子线程的运行循环上调用CFRunLoopStop(),此时CFRunLoopRun()在子线程中返回,子线程退出,主线程(这是阻止等待子线程退出)可以继续。 这似乎有效,但我的问题是:这是一种安全/推荐的方式吗? 特别是,从另一个线程调用CFRunLoopStop()容易导致竞争条件? 据我所知,Apple的文档对此主题保持沉默。 如果从主线程调用CFRunLoopStop()不是解决方案,那么什么是好的解决方案? 我知道我可以让子线程调用CFRunLoopRunInMode()并经常唤醒以检查布尔值或其他东西,但我宁愿不让子线程做任何轮询,如果我可以避免它。

是否需要互斥锁将不同的偏移量分配到已分配的堆内存中

我正在框架中放置一个生成二进制数据表的工具。 我计划让这个multithreading充分利用我所拥有的24个核心。 (我估计在一个线程中生成数据的时间大约为50天)。 我过去使用带有套接字通信的服务器/客户端设计完成了这项工作,因为我需要在多台计算机上进行分发。 这一次,我正在研究单机/multithreading方法,并试图找出如何以正确的方式做到这一点。 主线程将处理每个子线程的任务分配并确定到分配的内存中的偏移量。 每个线程将写入分配的内存中的唯一地址范围。 因为这些块在记录之间永远不会重叠,所以没有两个线程会尝试写入相同的偏移量。 void computeRecord(void *taskInput) { struct TaskData *taskData = (TaskData *)(taskInput); RecordData data; // A huge long computation block to populate data // (4-5 second run time) long record_id = taskData->record_id; char *buffer = taskData->start_buffer; // mutex lock needed here ?? int n_bytes = sizeof(RecordData) memcpy( (char *)(buffer+record_id*n_bytes), […]

与多任务下的UART FIFO相关的错误

附加程序:这些代码由我根据TI uart.c驱动程序库编写。 while(1){ //check if message on Queue -> read or check UART input if(uxQueueMessagesWaiting( UART_TASKQ ) != 0){ //may have bugs // deQueue xQueueReceive( UART_TASKQ, &UARTTaskHandle, 0x0A ); //do the task’s mission using the data in the stucture(put by control task) //Print out the input data. //**********debugging data /* testPointer = UARTTaskHandle->dataBuffer; testAmount = […]

在multithreading上下文中正确处理GetLastError(和其他)

假设GetLastError(和变体)是每个线程还是每个进程是正确的? 如果它是per-process的问题在multithreading应用程序中有些明显,因为没有办法保证在失败的调用和GetLastError之间没有进行其他Win32调用。 有时,GetLastError的值很重要。 例如,如果使用IO完成端口,则AcceptEx将返回FALSE(失败)。 WSAGetLastError(类似于GetLastError)将返回ERROR_IO_PENDING以通知您它已被挂起并且失败不是由于其他原因造成的。 问题是,有数十个其他呼叫可以在飞行中并覆盖此值。 这些调用是特定于线程还是特定于进程? 如果是特定于流程,那么如何正确处理?

实施关键部分

创建关键部分的方式更好,更快? 使用二进制信号量,在sem_wait和sem_post之间。 或者使用primefaces操作: #include void critical_code(){ static volatile bool lock = false; //Enter critical section while ( !__sync_bool_compare_and_swap (&lock, false, true ) ){ sched_yield(); } //… //Leave critical section lock = false; }

C语言中的线程间通信

作为我项目的一部分,我必须使用线程修改数值积分算法。 这大致是传统顺序方法中发生的情况。 void Controller(struct DE) { //initialization step for(;;) //till the entire range has not been covered… { //compute the next time-point solution using the previously known solutions. NIiter(); //then check if the result is acceptable or not and take the necessary steps… } } 现在这就是我打算做的…… void Controller(struct DE) { //initialization step for(;;) //till the […]