我被要求处理一段严重依赖 pthread 的代码。对这个库进行了很多调用,我对此一无所知。我已经学习了 pthread 的基础知识,并尝试了一些示例,例如创建连接等,但不知道它的深度。
我使用http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html学到了很多东西
上面的教程没有深入介绍初学者应该需要的 pthread 库。你们可以推荐一个网站或电子书让我看看并学习大多数高级内容,如信号处理、互斥锁、并发等。
TIA, the_Saint
我在 pthread 编程中遇到了一个奇怪的问题我在 vs2005 中用pthread-w32编译了以下代码
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <pthread.h>
#include <windows.h>
pthread_mutex_t lock;
void* thread1(void *) {
int r1;
while(true) {
pthread_mutex_lock(&lock); // rand is maybe a CS
r1 = rand() % 1500;
pthread_mutex_unlock(&lock);
Sleep(r1); printf("1:%d\n", r1);
}
return NULL;
}
void* thread2(void *) {
int r2;
while(true) {
pthread_mutex_lock(&lock);
r2 = rand() % 1500;
pthread_mutex_unlock(&lock);
Sleep(r2); printf("2:%d\n", r2);
}
return NULL;
}
int main() {
srand((int)time(NULL));
pthread_mutex_init(&lock, NULL);
pthread_t tc_p, tc_v;
pthread_create(&tc_p, NULL, …Run Code Online (Sandbox Code Playgroud) 进程运行卡在 32 000 (± 5%) 左右
~# cat /proc/sys/kernel/threads-max 127862
~# ulimit -s 堆栈大小 (kbytes, -s) 2048
可用内存:3,5 Go
此外,当我在进程像“top”一样卡住的情况下尝试基本命令时,我收到 bash 消息:无法分叉,内存不足。即使还有 3,5 Go 的空闲内存。
什么可以限制线程创建在 32 000 ?
我阅读了上述使用的术语,但它们的含义有隐含的差异,但我不知道我在哪里读到的。
你好,我试图在我的塔防中添加线程以使其更快,但现在速度变慢了。
代码结构非常简单
主要以 sdl opengl init 和 init 一切开始。然后游戏循环。无线程顺序: 1:keyboard and mouse event first 2:gameManager 3:drawGlScene
游戏管理器计算一切:移动怪物,攻击怪物,创建攻击动画和声音,检查你是赢了还是输了,如果波完成了,怪物产生,如果速度模式打开,这个功能会运行2次。和其他一些小功能。
绘图功能使用所有数据来绘制所有内容。使用绘图功能有 0 个数据修改
我使用的 cpu 是四核,这里是主要的可视化部分第一步初始化线程的东西
int main ( int argc, char** argv )
{
pthread_t t_engine;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
Run Code Online (Sandbox Code Playgroud)
然后所有其他初始化内容和游戏循环开始以 sdl 事件切换然后(仍在游戏循环中):
//calculate everything if we are in playing gamestate
if(id == MODE_PLAY)
{
rc = pthread_create(&t_engine, &attr, gameManager, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
//gameManager((void *)t);
}
//draw everything
DrawGLScene(); …Run Code Online (Sandbox Code Playgroud) 我写了这个小程序来理解pthread_create和pthread_join,但我不明白为什么data在thread_join之后变量的值会被改变.它在调用pthread函数后打印为0.
#include <pthread.h>
#include <stdio.h>
void* compute_prime (void* arg)
{
int n = *((int*) arg);
printf("Argument passed is %d\n",n);
return (void *)n;
}
int main ()
{
pthread_t thread;
int data = 5000;
int value=0;
pthread_create (&thread, NULL, &compute_prime, &data);
pthread_join (thread, (void*) &value);
printf("The number is %d and return value is %d.\n", data, value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
Argument passed is 5000
The number is 0 and return value is 5000.
Run Code Online (Sandbox Code Playgroud) 我理解pthread_join手册是我的程序应输出我提供的相同值,print_xs但获得的输出是:
Message from printxs 11131 a
Message from printxs 11234 b
32766 -16
32766 0
Run Code Online (Sandbox Code Playgroud)
该程序:
#include<stdio.h>
#include<pthread.h>
typedef struct paramsThread{
char c;
int count;
} threadPara;
void* print_xs(void* unused){
threadPara *tp = (threadPara *)unused;
int i=tp->count;
printf("Message from printxs %d %c\n", tp->count, tp->c);
return (void*)tp;
}
int main(){
pthread_t thread1, thread2;
threadPara t1,t2,t3,t4;
t1.c = 'a';
t2.c = 'b';
t1.count = 11131;
t2.count = 11234;
t3.count=0;
pthread_create(&thread1, NULL, &print_xs, &t1);
pthread_create(&thread2, NULL, &print_xs, &t2);
pthread_join(thread1,(void*)&t3); …Run Code Online (Sandbox Code Playgroud) 因此,假设1个线程,线程通过以下方式获取锁定:
pthread_mutex_lock(&lock);
Run Code Online (Sandbox Code Playgroud)
然后在解锁之前,它再次到达一条线:
pthread_mutex_lock(&lock);
Run Code Online (Sandbox Code Playgroud)
pthread库是否会阻止线程的进展,还是会识别出线程已经持有锁,从而让它通过?
我需要使用pthread_create来调用类型的C函数
int main_original(int argc, char** argv)
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情:
pthread_create(&t, NULL, main_original, NULL);
Run Code Online (Sandbox Code Playgroud)
编译器给我一个类型错误
从'int(*)(int,char**)'到'void*()(void)'的无效转换
那么,调用main_original以正确传递其参数的正确方法是什么?
我的问题与此类似。但是在查看了所有答案之后,我仍然不知道pthread_cancel()可以提供什么样的安全保证。所以我想问一个更具体的问题:
假设在名为my_thread的pthread_t变量上调用了pthread_cancel(),那么有可能在执行pthread_cancel(my_thread)时,已经以某种方式终止了与my_thread对应的实际线程,并且内核将my_thread的值回收用于另一个新创建的线程,例如通过执行pthread_cancel(my_thread),另一个意外线程被杀死了?