我正在尝试逐个像素地加密 BMP 图像,并且我使用 pthread 并行加密它们。
我的代码有点像这样:
struct arg_struct {
int arg1;
int arg2;
...
};
void* print_message(void* arguments) {
struct arg_struct* args = (struct arg_struct*)arguments;
//Encryption...
// exit from current thread
pthread_exit(NULL);
}
void multiThreadExample() {
cout << "Start..." << endl;
const int NUMBER_OF_THREADS = 50000; // number of pixels
pthread_t thread[NUMBER_OF_THREADS];
arg_struct arg[NUMBER_OF_THREADS];
for (int i=0;i<NUMBER_OF_THREADS;i++) {
arg[i].arg1 = i;
arg[i].arg2 = ... // give values to arguments in arg_struct
pthread_create(&thread[i], NULL, print_message, static_cast<void*>(&arg[i]));
}
for(int i = 0; …Run Code Online (Sandbox Code Playgroud) 当我运行以下程序时,输出为5.
为什么5?为什么不是8?
void *doit(void *vargp) {
int i = 3;
int *ptr = (int*)vargp;
(*ptr)++;
}
int main() {
int i = 0;
pthread_t tid;
pthread_create(&tid, NULL, doit, (void*)&i);
pthread_join(tid,NULL);
i = i + 4;
printf("%d",i);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试测试 9*9 数独问题的有效性,结果应通过printk(KERN_INFO "blablabla"). 我尝试了两种方法来编译我的 c 文件,make以及gcc myfile.c myfile -o -lpthread. 然而,他们都没有工作。
当我调用make它时,它抛出了一个致命错误: pthread.h: no such file or directory but it is right in the path /usr/include。
然后我从网上找到了一些建议,所以我gcc这次尝试了。不幸的是,我在调用后遇到了一堆错误,gcc myfile.c myfile -o -lpthread例如
/usr/include/linux/list.h: In function 'INIT_LIST_HEAD': /usr/include/linux/list.h:27:17: error: dereferencing pointer to incomplete type 'struct list_head'
和
error: unknown type name 'bool',
这仅仅是列举的一小部分。问题的关键是,我得到了这些错误后,我通过我的升级软件sudo apt-get update -y。
这是代码:
#include <pthread.h>
#include <linux/module.h>
#include <linux/kernel.h> …Run Code Online (Sandbox Code Playgroud) 我正在使用Ubuntu 12,当我在终端上编译代码时,我使用:
$ g++ -o ./myProgram ./main.cpp,然后
$ ./myProgram
我没有得到任何错误或警告,但它不会打印主函数之外的任何内容.
由于某种原因,似乎主函数中的pthread_creat命令不起作用.
这是我的程序代码:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
using namespace std;
pthread_mutex_t jobMutex;
key_t key = 5678;
#define SHMSZ 27
int shmid;
class Job {
public:
int speed, pleasant, easy;
//initialization, constructor, destructor
Job() {
}
Job(int new_s, int new_p, int new_e) {
speed = new_s;
pleasant = new_p;
easy = new_e;
}
~Job() { …Run Code Online (Sandbox Code Playgroud) 我想知道2003年的代码是否仍然是最先进的?它是使用PThreads的C++中的消费者和生产者示例.
http://www.mario-konrad.ch/wiki/doku.php?id=programming:multithreading:tutorial-06
如果没有,在C++中使用线程的现代方法是什么?
谢谢
我在下面有一个简单的程序,它打印创建的每个线程的线程ID.
#include <stdio.h>
#include <pthread.h>
#define NUM_THREAD 5
void *runner(void *param);
int main()
{
int i;
pthread_t tid[NUM_THREAD];
for(i = 0; i < NUM_THREAD; i++){
pthread_create(&tid[i], NULL, runner, NULL);
printf("%u\n", (unsigned int) pthread_self());
}
// for(i = 0; i < NUM_THREAD; i++)
// pthread_join(tid[i], NULL);
}
void *runner(void *param)
{
/* do some work */
pthread_exit(0);
}
Run Code Online (Sandbox Code Playgroud)
代码运行正常,给我正确的输出.
1527895872
1527895872
1527895872
1527895872
1527895872
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么线程ID是相同的?
我有一个长度为N的 std::string,我想使用线程将所有长度为K 的子字符串插入到 std::set 容器中。我应该使用多少个 std::thread 或 pthread_t 对象?
考虑 N = 500,000 和 K = 3。
我在共享内存段中使用一个简单的 pthreads 进程共享互斥锁来协调多个服务器实例。
代码是直接的:
在启动时服务器附连到共享存储器段(如果存在),或创建它,如果它不:
shm_open(),mmap(MAP_SHARED)等
这在测试时效果很好,但是一旦部署一段时间后,我就会遇到服务器实例根本不协调的情况。我可以通过在服务器启动后删除共享内存段来复制这一点:未来的服务器实例将创建/使用一个新段,但现有的一个被困在没有人使用的旧段中,因此实际上它与其余部分隔离......
所以我的猜测是 /dev/shm 中的共享内存段以某种方式被删除了,而不是我。这是唯一有意义的事情......这里发生了什么?
使用 linux 4.9 运行 debian。