‘PTHREAD_MUTEX_RECURSIVE’ undeclared

报错‘PTHREAD_MUTEX_RECURSIVE’ undeclared (first use in this function):

程序如下:

1 #include
2 #include
3 #include
4 #include
5

6 int main(int argc,char *argv[]){
7 pthread_mutex_t mutex;
8

9 if(argc < 2){
10 printf("-usage:%s [error|normal|recursive]\n",argv[0]);
11 exit(1);
12 }
13 pthread_mutexattr_t mutexattr;
14 pthread_mutexattr_init(&mutexattr);
15 if(!strcmp(argv[1],"error")){
16 pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_RECURSIVE);
17 }/*else if(!strcmp(argv[1],"normal")){
18 pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_NORMAL);
19 }else if(!strcmp(argv[1],"recursive")){
20 pthread_mutexattr_settype(&mutexattr,PTHREAD_MUTEX_RECURSIVE);
21 }
22 pthread_mutex_init(&mutex,&mutexattr);
23 if(pthread_mutex_lock(&mutex) != 0){
24 printf("lock failure\n");
25 }else {
26 printf("lock success\n");

27 }
28 if(pthread_mutex_lock(&mutex) != 0){
29 printf("lock failure\n");
30 }else {
31 printf("lock success\n");

32 }
33
34 pthread_mutex_unlock(&mutex);
35 pthread_mutex_unlock(&mutex);
36 pthread_mutexattr_destroy(&mutexattr);
37 pthread_mutex_destroy(&mutex); */
38
39
40 return 0;
41 }

请问怎么回事啊?谢谢

1个回答

你好!还是不知怎么改啊?

https://bbs.csdn.net/topics/260054382
这篇帖子有讨论递归锁编译时的未定义的错误问题。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
关于pthread_mutex多个线程忙等待问题?
文档说如果对一个已加锁的mutex再次加锁pthread_mutex_lock会形成一个等待队列。可是这个示例有点奇怪,我知道线程没有放弃cpu,可是为什么后面输出的hello不到数量? ``` #ifndef THREAD_POOL_H #define THREAD_POOL_H #include <pthread.h> #include <errno.h> #include <iostream> #include <unistd.h> #include <queue> class sample { public: void process(); }; template <typename T> class threadPool { public: threadPool(); void append(T &s); static void *worker(void *s); private: std::queue<T *> tasks; pthread_t threads[5]; bool run = true; pthread_mutex_t t; }; // template <typename T> // void threadPool<T>::append(T& s) // { // pthread_t t; // pthread_create(&t,nullptr,worker,&s); // pthread_detach(t); // } template <typename T> threadPool<T>::threadPool() : tasks() { pthread_mutex_init(&t,nullptr); for(int i=0;i<5;++i) { pthread_create(&threads[i],nullptr,worker,this); pthread_detach(threads[i]); } } template <typename T> void threadPool<T>::append(T &s) { pthread_mutex_lock(&t); tasks.push(&s); printf("%d append.\n",pthread_self()); pthread_mutex_unlock(&t); } template <typename T> void *threadPool<T>::worker(void *s) { auto pool=static_cast<threadPool<T>*>(s); while (pool->run) { pthread_mutex_lock(&(pool->t)); if (pool->tasks.empty()) { pthread_mutex_unlock(&(pool->t)); printf("%d get the lock but continue.\n",pthread_self()); continue; } auto work = pool->tasks.front(); pool->tasks.pop(); printf("%d get the lock and print\n",pthread_self()); pthread_mutex_unlock(&(pool->t)); work->process(); } return nullptr; } #endif //!THREAD_POOL_H ``` ``` #include "thread_pool.h" void sample::process() { std::cout<<"hello world.\n"<<std::endl; } ``` ``` #include "thread_pool.h" #include <iostream> int main(int argc, char **argv) { threadPool<sample> pool; sample d[100]; for(int i=0;i<100;++i) { pool.append(d[i]); } return 0; } ``` ``` 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1268307712 get the lock but continue. 1285158720 append. 1268307712 get the lock and print 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1243129600 get the lock but continue. 1259915008 get the lock but continue. 1251522304 get the lock but continue. hello world. 1285158720 append. 1268307712 get the lock and print hello world. 1268307712 get the lock but continue. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1251522304 get the lock and print hello world. 1251522304 get the lock and print hello world. 1259915008 get the lock and print 1243129600 get the lock and print hello world. 1243129600 get the lock and print hello world. 1243129600 get the lock and print hello world. 1243129600 get the lock and print hello world. 1243129600 get the lock and print hello world. 1285158720 append. 1251522304 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1285158720 append. 1234736896 get the lock and print hello world. 1259915008 get the lock and print hello world. 1259915008 get the lock and print hello world. 1259915008 get the lock and print hello world. hello world. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1251522304 get the lock and print hello world. 1251522304 get the lock and print hello world. 1251522304 get the lock and print hello world. 1251522304 get the lock and print hello world. 1251522304 get the lock and print hello world. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1251522304 get the lock but continue. 1268307712 get the lock but continue. 1259915008 get the lock but continue. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1234736896 get the lock and print hello world. 1234736896 get the lock and print hello world. 1243129600 get the lock and print hello world. 1243129600 get the lock and print hello world. 1243129600 get the lock and print hello world. 1243129600 get the lock and print hello world. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1243129600 get the lock but continue. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. 1285158720 append. ``` **头晕了一下午,麻烦大佬出来解救众生。**
pthread_cond_signal用法问题
请看下述代码是否有问题 疑问点:是否可以主线程触发->A->B->C->A(->表示pthread_cond_signal) ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <string.h> using namespace std; pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER; int count = 10; int thd_id = 0; void * thread_func1(void * arg) { for(int i = 0; i < count; i++) { pthread_mutex_lock(&mutex1); pthread_cond_wait(&cond1, &mutex1); printf("A"); pthread_mutex_unlock(&mutex1); // notify B pthread_cond_signal(&cond2); } return (void*)0; } void * thread_func2(void * arg) { for(int i = 0; i < count; i++) { pthread_mutex_lock(&mutex2); pthread_cond_wait(&cond2, &mutex2); printf("B"); pthread_mutex_unlock(&mutex2); // notify C pthread_cond_signal(&cond3); } return (void*)0; } void * thread_func3(void * arg) { for(int i = 0; i < count; i++) { pthread_mutex_lock(&mutex3); pthread_cond_wait(&cond3, &mutex3); printf("C"); pthread_mutex_unlock(&mutex3); // notify A pthread_cond_signal(&cond1); } return (void*)0; } int main() { pthread_t thd1, thd2, thd3; if(pthread_create(&thd1, NULL, thread_func1, NULL) != 0) { printf("Create thread failed.\n"); } if(pthread_create(&thd2, NULL, thread_func2, NULL) != 0) { printf("Create thread failed.\n"); } if(pthread_create(&thd3, NULL, thread_func3, NULL) != 0) { printf("Create thread failed.\n"); } // first notify A pthread_cond_signal(&cond1); void *ret; pthread_join(thd1, &ret); pthread_join(thd2, &ret); pthread_join(thd3, &ret); return 0; } ```
pthread_create后子线程输出了两次
#include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <pthread.h> void *start_handler(void *); int main(void) { pthread_t tid; int err; if(err = pthread_create(&tid, NULL, start_handler, NULL)) { fprintf(stderr, "Create thread: %s\n", strerror(err)); return -1; } printf("OK\n"); return 0; } void *start_handler(void *arg) { int err; if(err = pthread_detach(pthread_self())) { fprintf(stderr, "Detach thread: %s\n", strerror(err)); pthread_exit((void *)-1); } printf("Start!\n"); pthread_exit((void *)1); }
linux两线程 使用pthread_kill会带来什么影响
linux同一进程的两个线程A,B,A是更新操作,B是下载,当下载时,若有更新操作,则让更新线程A利用pthread_kill操作结束B进程,此时对B下载的文件有什么影响?
windows下使用pthread.h库的问题
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <ctype.h> #include <windows.h> pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t empty = PTHREAD_COND_INITIALIZER; pthread_cond_t full = PTHREAD_COND_INITIALIZER; char buf[256]; int main() { pthread_t t1,t2; void * put_buf(void *); void * get_buf(void *); pthread_mutex_lock(&lock); pthread_cond_signal(&empty); pthread_create(&t1, NULL, put_buf, NULL); pthread_create(&t2, NULL, get_buf, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; } void *put_buf() { while (true) { pthread_cond_wait(&empty, &lock); printf_s("empty flag is raised\n"); pthread_mutex_lock(&lock); printf_s("input: \n"); gets_s(buf); pthread_mutex_unlock(&lock); pthread_cond_signal(&full); } } void *get_buf(){ Sleep(2); while (true) { pthread_cond_wait(&full, &lock); printf_s("full flag is raised"); pthread_mutex_lock(&lock); printf_s("output:\n"); pthread_cond_signal(&empty); } } ``` ``` 出现问题:错误 1 error LNK2019: 无法解析的外部符号 "void * __cdecl put_buf(void *)" (?put_buf@@YAPAXPAX@Z),该符号在函数 _main 中被引用 c:\Users\陌桑时代shine\documents\visual studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.obj ConsoleApplication2 错误 2 error LNK2019: 无法解析的外部符号 "void * __cdecl get_buf(void *)" (?get_buf@@YAPAXPAX@Z),该符号在函数 _main 中被引用 c:\Users\陌桑时代shine\documents\visual studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\源.obj ConsoleApplication2
C语言多线程运行时间计算问题
如题。计算着两个线程执行的总时间,如何计算。 ``` #include <stdio.h> #include <stdlib.h> #include <pthread.h> int x; pthread_mutex_t mutex; void thread1(void) { while(x>0) { pthread_mutex_lock(&mutex); printf("Thread 1 is running x=%d/n",x); x--; pthread_mutex_unlock(&mutex); sleep(1); } pthread_exit(NULL); } void thread2(void) { while(x>0) { pthread_mutex_lock(&mutex); printf("Thread 2 is running x=%d/n",x); x--; pthread_mutex_unlock(&mutex); sleep(1); } pthread_exit(NULL); } int main() { pthread_t id1,id2; int ret; ret = pthread_mutex_init(&mutex,NULL); if(ret!=0) { printf("pthread_mutex_init error/n"); exit(0); } x=10; ret = pthread_create(&id1,NULL,(void *)thread1,NULL); if(ret!=0) { printf("Create pthread 1 error/n"); exit(0); } ret = pthread_create(&id2,NULL,(void *)thread2,NULL); if(ret!=0) { printf("Create pthread 2 error/n"); exit(0); } pthread_join(id1,NULL); pthread_join(id2,NULL); printf("Done/n"); return 0; } ```
error: ‘class Stack’ has no member named ‘elem’
``` #include <pthread.h> class Stack{ public: Stack(); ~Stack(); int pop(); void push(int); private: static const int STACKSIZE; int elem[STACKSIZE]; int top; pthread_mutex_t mutex; pthread_cond_t notfull, notempty; }; ``` ``` #include "stack.h" #include <pthread.h> Stack::Stack(): top(-1), mutex(PTHREAD_MUTEX_INITIALIZER), notfull(PTHREAD_COND_INITIALIZER), notempty(PTHREAD_COND_INITIALIZER) {} /* same effect as lock while (isfull()); doit; unlock */ const int Stack::STACKSIZE = 10; void Stack::push(int v) { pthread_mutex_lock(&mutex); while (top == STACKSIZE) {//full? pthread_cond_wait(&notfull, &mutex); } this->elem[++top] = v; pthread_mutex_unlock(&mutex); pthread_cond_broadcast(&notempty); } int Stack::pop() { pthread_mutex_lock(&mutex); while (top == -1) {//empty? pthread_cond_wait(&notempty, &mutex); } int retval = elem[top--]; pthread_mutex_unlock(&mutex); pthread_cond_broadcast(&notfull); return retval; } ```
关于C++ 单例模式的double-check的疑问。
最近在学习单例模式,在了解频繁加锁会造成资源的浪费的前提下,通常做法时使用 双重检查的机制来解决资源浪费。于是我做了以下测试: ```c++ #include <unistd.h> #include <pthread.h> #include <iostream> #include <vector> #include <sys/time.h> using namespace std; pthread_mutex_t lo; class Singleton { public: static Singleton * p_instance; static Singleton * GetInstance(); private: Singleton(){cout<<"Contructor running"<<endl;} }; Singleton * Singleton::p_instance=NULL; Singleton * Singleton::GetInstance() { if(p_instance==NULL) { pthread_mutex_lock(&lo); if(p_instance==NULL) p_instance=new Singleton(); pthread_mutex_unlock(&lo); } pthread_mutex_unlock(&lo); return p_instance; } void * fun(void *) { Singleton::GetInstance(); } int main() { //cout<<&lo<<endl; pthread_mutex_init(&lo,NULL); vector<pthread_t *> thread_pool; for(int i=0;i<10000000;i++) { pthread_t * pth = new pthread_t; thread_pool.push_back(pth); } //calculate time duration struct timeval t1,t2; double duration=0; gettimeofday(&t1,NULL); for(int i=0;i<thread_pool.size();i++) { pthread_create(thread_pool[i],NULL,fun,NULL); } for(int i=0;i<thread_pool.size();i++) pthread_join(*thread_pool[i],NULL); gettimeofday(&t2,NULL); duration = t2.tv_sec - t1.tv_sec + (t2.tv_usec - t1.tv_usec)/1000000.0; cout<<duration<<endl; return 0; } ``` 结果在ubuntu18.04中测试耗时:14.907s 然后我将程序中的,第一个判空去掉,就像这样: ```c++ Singleton * Singleton::GetInstance() { //if(p_instance==NULL) //{ pthread_mutex_lock(&lo); if(p_instance==NULL) p_instance=new Singleton(); // pthread_mutex_unlock(&lo); //} pthread_mutex_unlock(&lo); return p_instance; } ``` 同样地,进行了多次返回的结果是: ``` Contructor running 18.1153 me@ubuntu:~/Desktop/pocesstet$ ./app Contructor running 14.9578 me@ubuntu:~/Desktop/pocesstet$ ./app Contructor running 14.8444 me@ubuntu:~/Desktop/pocesstet$ ./app Contructor running 14.3856 ``` 时间时间甚至比使用双重检查(double check)的效果要好?这是为什么? 是我哪里理解错了吗?先谢谢各位了!
多线程复制程序的求解
我写了一个多线程复制程序遇到下面问题,不知道该怎么解决,求各位大神cp_thread_mutex.c: 在函数‘main’中: cp_thread_mutex.c:98: 警告:传递‘pthread_mutex_destroy’的第 1 个参数时在不兼容的指针类型间转换 /usr/include/pthread.h:738: 附注:需要类型‘union pthread_mutex_t *’,但实参的类型为‘union pthread_mutex_t ***’ cp_thread_mutex.c: 在函数‘reader’中: cp_thread_mutex.c:116: 警告:传递‘pthread_mutex_lock’的第 1 个参数时在不兼容的指针类型间转换 /usr/include/pthread.h:746: 附注:需要类型‘union pthread_mutex_t *’,但实参的类型为‘union pthread_mutex_t **’ cp_thread_mutex.c:128: 警告:传递‘pthread_mutex_unlock’的第 1 个参数时在不兼容的指针类型间转换 /usr/include/pthread.h:757: 附注:需要类型‘union pthread_mutex_t *’,但实参的类型为‘union pthread_mutex_t **’ cp_thread_mutex.c: 在函数‘writer’中: cp_thread_mutex.c:152: 警告:传递‘pthread_mutex_lock’的第 1 个参数时在不兼容的指针类型间转换 /usr/include/pthread.h:746: 附注:需要类型‘union pthread_mutex_t *’,但实参的类型为‘union pthread_mutex_t **’ cp_thread_mutex.c:164: 警告:传递‘pthread_mutex_unlock’的第 1 个参数时在不兼容的指针类型间转换 /usr/include/pthread.h:757: 附注:需要类型‘union pthread_mutex_t *’,但实参的类型为‘union pthread_mutex_t **’ 下面是代码: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <pthread.h> #include <dirent.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> #include <time.h> //#include "cp_header.h" #define BUFFER_SIZE 5 #define THREAD_BUFFER_SIZE 1024 struct prodcons { char** buffer; pthread_mutex_t** lock; int infd,outfd; int read_bytes,write_bytes; int thread_file_size; int* num; //pthread_cond_t notempty; //pthread_cond_t notfull; }; size_t get_filesize(int fd) { struct stat st; fstat(fd,&st); return st.st_size; } void *reader(void *arg1); void *writer(void *arg2); int main(int argc,char *argv[]) { int infd,outfd; int k=0; int res; pthread_t th_a,th_b; if(argc!=3) { exit(1); } infd=open(argv[1],O_RDONLY); outfd=open(argv[2],O_CREAT|O_WRONLY,0644); if(infd==-1||outfd==-1) { printf("fail to open\n"); return -1; } else { printf("open success!!!!!\n"); } size_t file_size=get_filesize(infd); printf("file size is %d\n",file_size); struct prodcons *b=(struct prodcons *) malloc(BUFFER_SIZE*sizeof(struct prodcons)); for(int i=0;i<BUFFER_SIZE;i++) { b->lock[i]=(pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); res=pthread_mutex_init(b->lock[i],NULL); b->buffer[i]=(char*)malloc(THREAD_BUFFER_SIZE*BUFFER_SIZE); b->num[i]=0; } b->infd=infd; b->outfd=outfd; b->thread_file_size=file_size; printf("file_size is %d\n",b->thread_file_size); //pthread_mutex_init(&b->lock, NULL); //pthread_cond_init(&b->notempty,NULL); //pthread_cond_init(&b->notfull,NULL); //b->read_bytes=0; //b->write_bytes=0; pthread_create(&th_a,NULL,reader,(void *)b); pthread_create(&th_b,NULL,writer,(void *)b); pthread_join(th_a,NULL); pthread_join(th_b,NULL); free(b); for(int i=0;i<BUFFER_SIZE;i++) { free(b->lock[i]); free(b->buffer[i]); } close(infd); close(outfd); for(int i=0;i<BUFFER_SIZE;i++) { pthread_mutex_destroy(&b->lock); } return 0; } void *reader(void *arg1) { //int read_bytes; int i=0; static int count=0; int ret; //char buffer[THREAD_BUFFER_SIZE]; struct prodcons *b=(struct prodcons *)arg1; while(count<b->thread_file_size) { //pthread_mutex_lock(&b->lock); for(i=1;i<BUFFER_SIZE;i++) { pthread_mutex_lock(&b->lock[i]); b->read_bytes=read(b->infd,b->buffer[i],THREAD_BUFFER_SIZE); if((b->read_bytes==-1)&&(errno!=EINTR)) { printf("fail to read\n"); break; } else if(b->read_bytes>0) { b->num[i]=b->read_bytes; count+=b->read_bytes; i++; printf("read_bytes is %d\n",b->num[i]); } pthread_mutex_unlock(&b->lock[i]); sleep(1); } if (i>=BUFFER_SIZE) { i=0; } } /* pthread_mutex_unlock(&b->lock);*/ } void *writer(void *arg2) { struct prodcons *b=(struct prodcons *)arg2; //char buffer[THREAD_BUFFER_SIZE]; //int write_bytes,data; int i=0; int ret1; static int count1=0; while(count1<b->thread_file_size) { //pthread_mutex_lock(&b->lock); for(i=0;i<BUFFER_SIZE;i++) { if(count1<b->thread_file_size) { pthread_mutex_lock(&b->lock[i]); //data=buffer; b->write_bytes=write(b->outfd,b->buffer[i],b->num[i]); if((b->write_bytes==-1)&&(errno!=EINTR)) { break; } else if(b->write_bytes>0) { count1+=b->write_bytes; i++; printf("write_bytes is %d\n",b->write_bytes); } pthread_mutex_unlock(&b->lock[i]); sleep(1); } else { break; } //pthread_mutex_unlock(&b->lock); //sleep(1); } if(i>=BUFFER_SIZE) { i=1; } /*if(pthread_mutex_unlock(&b->lock)!=0) { perror("pthread_mutex_unlock"); } else { printf("pthread2 unlock the variable\n"); } sleep(1);*/ //pthread_mutex_destroy(&b->lock); } /*pthread_cond_signal(&b->notfull);*/ /*pthread_mutex_unlock(&b->lock);*/ }
C++互斥锁迷惑的点,求大神们解答
使用pthread_mutex_t mutex = NULL和 pthread_mutex_t mutex = PTHREAD_M UTEX_INITIALIZER进行初始化互斥对象, 1、使用NULL的好像没有生效(意思是没有锁住互斥对象),是这样理解的吗? 2、使用这种静态初始化互斥对象,不需要使用pthread_mutex_destroy()进行销毁, 而使用动态初始化的方式进行初始化互斥对象的话需要使用这个destroy(),是我这样理解的吗。
Linux死锁多线程编程在qt中运行异常
用ubuntu虚拟机写qt,在MainWindow类中写的好几个相关死锁代码总是异常中断,但把这部分代码单独提出写在一个cpp中就能编译通过并且正常输出 ``` 类里死锁代码部分 (简化了一下,只粘了最基本的一个死锁多线程) //mainwindow.h namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0); ~MainWindow(); private: Ui::MainWindow *ui; static void* Q2(void *arg); static void* P2(void *arg); void InitializedTable_unlock(); void choose(int); public slots: void unlock(); }; #endif // MAINWINDOW_H //mainwindow.cpp pthread_t tid[2]; // 静态初始化互斥量 pthread_mutex_t mutexA = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutexB = PTHREAD_MUTEX_INITIALIZER; MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pushButton_3_1_b,SIGNAL(clicked()),this,SLOT(unlock())); } MainWindow::~MainWindow() { delete ui; } void* MainWindow::P1(void *arg) { MainWindow w; pthread_mutex_lock(&mutexA); //P1 get mutexA //choose按钮对应移动函数 w.choose(1); usleep(1000); pthread_mutex_lock(&mutexB); //P1 get mutexB w.choose(3); pthread_mutex_unlock(&mutexA); //P1 release mutexB w.choose(4); pthread_mutex_unlock(&mutexB); //P1 release mutexA w.choose(5); return NULL; } void* MainWindow::Q1(void *arg) { MainWindow w; pthread_mutex_lock(&mutexB); //Q1 get mutexB w.choose(2); usleep(1000); pthread_mutex_lock(&mutexA); //Q1 get mutexA w.choose(6); pthread_mutex_unlock(&mutexB); //Q1 release mutexA w.choose(7); pthread_mutex_unlock(&mutexA); //Q1 release mutexB w.choose(8); return NULL; } void MainWindow::lock(void) { MainWindow m; // 创建线程 1 pthread_create(&(tid[0]), NULL, m.Q1, NULL ); // 创建线程 2 pthread_create(&(tid[1]), NULL, m.P1, NULL); pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); } // 死锁资源按钮相应位移函数 void MainWindow::choose(int step){ if(step == 1){ //P1 get mutexA ......全都写的是对应的button进行位移的代码 } else if(step == 2){ //Q1 get mutexB ...... } else if(step == 3){ //P2 release mutexA ...... } else if(step == 4){ //P2 get mutexB ...... } else if(step == 5){ //P2 release mutexB ...... } else if(step == 6){ //Q2 get mutexA ...... } else if(step == 7){ //Q2 release mutexB ...... } else if(step == 8){ //Q2 release mutexA QPropertyAnimation *animation12 = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry"); animation12->setDuration(1000); animation12->setStartValue(QRect(110,280,141,71)); animation12->setEndValue(QRect(110,140,141,71)); animation12->start(); QPropertyAnimation *animation13 = new QPropertyAnimation(ui->pushButton_2_1_1,"geometry"); animation13->setDuration(1000); animation13->setStartValue(QRect(110,140,141,71)); animation13->setEndValue(QRect(530,140,141,71)); animation13->start(); } } 这部分代码运行不出来,不报错,但click一下那个unlock就异常中断 然后我把这部分代码单独整理成一个cpp就能跑 #include <QCoreApplication> #include<pthread.h> #include<semaphore.h> #include<unistd.h> //用于sleep() pthread_t tid[2]; // 静态初始化互斥量 pthread_mutex_t mutexA = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutexB = PTHREAD_MUTEX_INITIALIZER; void* P1(void *arg) { pthread_mutex_lock(&mutexA); printf("P1 get mutexA\n"); usleep(1000); pthread_mutex_unlock(&mutexB); printf("P1 release mutexA\n"); pthread_mutex_lock(&mutexB); printf("P1 get mutexB\n"); pthread_mutex_unlock(&mutexA); printf("P1 release mutexB\n"); return NULL; } void* Q1(void *arg) { pthread_mutex_lock(&mutexB); printf("Q1 get mutexB\n"); usleep(1000); pthread_mutex_lock(&mutexA); printf("Q1 get mutexA\n"); pthread_mutex_unlock(&mutexB); printf("Q1 release mutexA\n"); pthread_mutex_unlock(&mutexA); printf("Q1 release mutexB\n"); return NULL; } int main() { // 创建线程 1 pthread_create(&(tid[0]), NULL, &Q1, NULL ); // 创建线程 2 pthread_create(&(tid[1]), NULL, &P1, NULL); pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); return 0; } 结果也是我想要的 P1 get mutexA Q1 get mutexB P1 release mutexA P1 get mutexB P1 release mutexB Q1 get mutexA Q1 release mutexA Q1 release mutexB ```
各位大侠,小弟练习写了个代码,求解答
为什么运行的结果不能输出“>”,而且当我在终端输入quit时 出现不停打印“>”的死循环 代码如下: #include<stdio.h> #include<pthread.h> #include<string.h> #include<stdlib.h> #include<time.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #define MAX 1024 pthread_mutex_t rwlock = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t rwcond =PTHREAD_COND_INITIALIZER; void* read_file(void *arg) { int fd; int ret,n; char buf[MAX]; if(fd = open ((char *)arg ,O_CREAT|O_RDONLY,0666) == -1) { perror("open fail"); pthread_exit(NULL); } while(1){ ret =pthread_mutex_lock(&rwlock); if (ret != 0) { perror("pthread_mutex_lock"); close(fd); pthread_exit(NULL); } n = read(fd ,buf,sizeof(buf)); if(n< 0) { perror("read fail"); close(fd); pthread_exit(NULL); } while(n == 0) { ret = pthread_cond_wait(&rwcond,&rwlock); if(ret != 0) { perror("pthread_cond_wait fail"); close(fd); pthread_exit(NULL); } } buf[n] ='\0'; printf("read %d character :%s \n",strlen(buf),buf); if(strncmp(buf,"quit",4) == 0) { ret =pthread_mutex_unlock(&rwlock); if(ret !=0) { perror("pthread_mutex_unlock fail"); close(fd); pthread_exit(NULL); } break; } ret = pthread_mutex_unlock(&rwlock); if(ret != 0) { perror("pthread_mutex_unlock fail"); close(fd); pthread_exit(NULL); } } close(fd); pthread_exit(NULL); } void* write_file(void *arg) { int fd; int ret,n; char buf[MAX]; char buf_bak[MAX]; fd = open((char*)arg,O_CREAT|O_TRUNC|O_WRONLY,0666); if(fd < 0) { perror("open fail"); pthread_exit(NULL); } while(1){ ret = pthread_mutex_lock(&rwlock); if(ret != 0) { perror("pthread_mutex_lock fail"); close(fd); pthread_exit(NULL); } printf(">\n"); fgets(buf,strlen(buf),stdin); //snprintf(buf_bak,sizeof(buf_bak),"%s",buf); buf[strlen(buf)-1] ='\0'; n = write(fd,buf,sizeof(buf)); if(n < 0) { perror("write fail"); close(fd); pthread_exit(NULL); } if(strncmp(buf,"quit",4) == 0) { ret = pthread_mutex_unlock(&rwlock); if(ret != 0) { perror("pthread_mutex_unlock fail"); close(fd); pthread_exit(NULL); } ret = pthread_cond_signal(&rwcond); if(ret != 0) { perror("pthread_cond_signal fail"); close(fd); pthread_exit(NULL); } usleep(500); break; } ret = pthread_mutex_unlock(&rwlock); if(ret != 0) { perror("pthread_mutex_unlock fail"); close(fd); pthread_exit(NULL); } ret =pthread_cond_signal(&rwcond); if(ret != 0) { perror("pthread_cond_signal fail"); close(fd); pthread_exit(NULL); } usleep(500); } close(fd); pthread_exit(NULL); } int main (int argc,char *argv[]) { pthread_t rpth_id,wpth_id; int ret; if(argc < 2) { fprintf(stderr,"usage: %s argv[1]\n",argv[0]); exit(EXIT_FAILURE); } ret = pthread_create(&rpth_id,NULL,read_file,(void *)argv[1]); if(ret != 0) { perror("prhread_create fail"); exit(EXIT_FAILURE); } usleep(500); ret = pthread_create(&wpth_id,NULL,write_file,(void *)argv[1]); if (ret != 0) { perror("pthread_create fail"); exit(EXIT_FAILURE); } pthread_join(rpth_id,NULL); pthread_join(wpth_id,NULL); return 0; }
两个线程打印到100,帮我看下为啥会打印到101
#include <stdio.h> #include <stdlib.h> #include <pthread.h> int N = 100; int n = 0; pthread_mutex_t mutex; void *func1() { while (n < N+1) { pthread_mutex_lock(&mutex); printf ("%d 1\n", n); n++; pthread_mutex_unlock(&mutex); } if (n == N+1) pthread_exit(NULL); } void *func2() { while (n < N+1) { pthread_mutex_lock(&mutex); printf ("%d 2\n", n); n++; pthread_mutex_unlock(&mutex); } if (n == N+1) pthread_exit(NULL); } int main(void) { pthread_t pid1, pid2; pthread_mutex_init(&mutex, NULL); if ((pthread_create(&pid1, NULL, func1, NULL)) != 0) printf ("pthread create fail\n"); if ((pthread_create(&pid2, NULL, func2, NULL)) != 0) printf ("pthead create fail\n"); pthread_join(pid1, NULL); pthread_join(pid2, NULL); return 0; } 输出如下: 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 2 26 2 27 2 28 2 29 2 30 2 31 2 32 2 33 2 34 2 35 2 36 2 37 2 38 2 39 2 40 2 41 2 42 2 43 2 44 2 45 2 46 2 47 2 48 2 49 2 50 2 51 2 52 2 53 2 54 2 55 2 56 2 57 2 58 2 59 2 60 2 61 2 62 2 63 2 64 2 65 2 66 2 67 2 68 2 69 2 70 2 71 2 72 2 73 2 74 2 75 2 76 2 77 2 78 2 79 2 80 2 81 2 82 2 83 2 84 2 85 2 86 2 87 2 88 2 89 2 90 2 91 2 92 2 93 2 94 2 95 2 96 2 97 2 98 2 99 2 100 2 101 1 还有一个问题,当我把pthread_mutex_init(&mutex, NULL);放到main函数外面时,编译一种报错 报错代码如下: a.c:8:20: error: expected declaration specifiers or ‘...’ before ‘&’ token pthread_mutex_init(&mutex, NULL); ^ In file included from /usr/include/time.h:37:0, from /usr/include/pthread.h:24, from a.c:3: a.c:8:28: error: expected declaration specifiers or ‘...’ before ‘(’ token pthread_mutex_init(&mutex, NULL);
linux下多线程加锁 编译通不够
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include<stdio.h> #include<pthread.h> #include<unistd.h> #include<stdlib.h> void *thread_function(void *arg); int run_now = 1; pthread_mutex_t work_mutex; int main() { int res; int print_count1 = 0; pthread_t a_thread; if(pthread_mutex_init(&work_mutex,NULL) != 0) { perror("Mutex init failed"); exit(1); } if(pthread_mutex_lock(&work_mutex) !=0) { perror("Lock failed"); exit(1); } else printf("main lock\n"); while(print_count1++<5) { if(run_now == 1) { printf("main thread is run\n"); run_now = 2; } else { printf("main thread is sleep\n"); sleep(1); } } if(pthread_mutex_unlock(&work_mutex) !=0) { perror("unlock failed"); exit(1); } else printf("main unlock\n"); pthread_mutex_destory(&work_mutex); pthread_join(a_thread,NULL); exit(0); } void *thread_function(void *arg) { int print_count2 = 0; sleep(1); if(pthread_mutex_lock(&work_mutex) != 0) { perror("Lock failed"); exit(1); } else printf("function lock\n"); while(print_count2++<5) { if(run_now == 2) { printf("function thread is run\n"); run_now = 1; } else { printf("function thread is sleep\n"); sleep(1); } } if(pthread_mutex_unlock(&work_mutex) != 0) { perror("unlock failed"); exit(1); } else printf("function unlock\n"); pthread_exit(NULL); } ![图片说明](https://img-ask.csdn.net/upload/201705/09/1494259668_862499.jpg)
互斥锁的销毁时需要注意的问题
在APUE一书中,一个对互斥锁销毁的例子如下: void foo_hold(struct foo *fp) { pthread_mutex_lock(&fp->f_lock); fp->f_count++; pthread_mutex_unlock(&fp->f_lock); } void foo_rele(struct foo *fp) { pthread_mutex_lock(&fp->f_lock); if (--fp->f_count == 0) { pthread_mutex_unlock(&fp->f_lock); pthread_mutex_destroy(&fp->f_lock); free(fp); } else { pthread_mutex_unlock(&fp->f_lock); } } 在foo_rele()函数中,以foo结构中count为条件进行该结构的销毁操作,我现在的疑问是书中也提到了在这个销毁过程中也可能会有线程阻塞在该锁上,可以确保对象在释放内存前不会被找到这种方式来避免,那么怎么确保对象不会被找到呢?通过指针,还是有什么方法?还望高手不离赐教!
linux多线程的初级问题
#include<stdio.h> #include<stdlib.h> #include<pthread.h> void *test(void *arg); pthread_mutex_t mutex; int main() { pthread_t t_id; for(int i=0;i<50;i++) { pthread_create(&t_id,NULL,test,(void*)&i); } pthread_mutex_destroy(&mutex); } void *test(void *arg) { pthread_detach(pthread_self()); int i=*(int*)arg; printf("%d:\n",i); return NULL; } 请前辈看看这段代码,为什么输出会有重复的数字,有时候输出数量还会少于50,当把pthread_detach()换为pthread_join()的时候就很正常了,求解答。
C/C++线程池问题,销毁线程时程序段出错
在测试函数中调用StopAll线程退出失败,产生SIGSEGV信号,原因是什么? ``` #include "threadpool.h" #include <iostream> #include <vector> using namespace std; CTask::CTask(string name) { this->m_strTaskName = name; this->m_ptrData = NULL; } void CTask::setdata(void *data) { this->m_ptrData = data; } /********************************************************************************************************************************************/ /** @static 变量在类外进行初始化 */ vector<CTask *> CThreadPool::m_vecTaskList; bool CThreadPool::shutdown = false; pthread_mutex_t CThreadPool::m_pthreadMutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t CThreadPool::m_pthreadCond = PTHREAD_COND_INITIALIZER; /** @ 构造函数初始化要创建线程池里的线程数量 */ CThreadPool::CThreadPool(int threadNum) { this->m_iThreadNum = threadNum; this->CreatePool(); } /** @ 向任务队列中添加任务,需要进行加锁处理 @ 成功返回0 */ int CThreadPool::AddTask(CTask *task) { pthread_mutex_lock(&m_pthreadMutex); this->m_vecTaskList.push_back(task); cout << "add task successful the task vector size is" << this->getTaskSize() << endl; pthread_mutex_unlock(&m_pthreadMutex); pthread_cond_signal(&m_pthreadCond); return 0; } /** @ 唤醒所有线程让其退出并且销毁互斥锁与条件变量释放内存空间 @ 重复关闭返回-1 @ 成功关闭返回0 */ int CThreadPool::StopAll() { if(shutdown) { return -1; } shutdown = true; pthread_cond_broadcast(&m_pthreadCond); for(int i = 0; i < m_iThreadNum; ++ i) { pthread_join(pthread_id[i], NULL); } // delete[] pthread_id; // pthread_mutex_destroy(&m_pthreadMutex); // pthread_cond_destroy(&m_pthreadCond); return 0; } /** @ 返回当前任务列表里面的任务数量 */ int CThreadPool::getTaskSize() { return this->m_vecTaskList.size(); } /** @ 创建线程 @ 成功返回0 */ int CThreadPool::CreatePool() { this->pthread_id = new pthread_t(m_iThreadNum); for(int i = 0; i < this->m_iThreadNum; ++ i) { pthread_create(&pthread_id[i], NULL, ThreadFunc, NULL); } return 0; } void *CThreadPool::ThreadFunc(void *arg) { pthread_mutex_lock(&m_pthreadMutex); pthread_t tid = pthread_self(); pthread_mutex_unlock(&m_pthreadMutex); cout << tid << endl; while(1) { pthread_mutex_lock(&m_pthreadMutex); while(m_vecTaskList.size() == 0 && !shutdown) { cout << "cond_wait id is:" << tid << endl; pthread_cond_wait(&m_pthreadCond, &m_pthreadMutex); } if(shutdown) { cout << "thread " << tid << " exit" << endl; pthread_mutex_unlock(&m_pthreadMutex); pthread_exit(NULL); } vector<CTask *>::iterator iter = m_vecTaskList.begin(); CTask *temp = NULL; if(iter != m_vecTaskList.end()) { temp = *iter; m_vecTaskList.erase(iter); } pthread_mutex_unlock(&m_pthreadMutex); temp->run(); } pthread_exit(NULL); } int main (void) { cout << "begin............." << endl; CThreadPool pool(5); for(int i = 0; i < 50; ++ i) { CTask *task = new CTask(); pool.AddTask(task); } while(pool.getTaskSize() != 0) { cout << pool.getTaskSize() << endl; continue; } pool.StopAll(); cout << "end..............." << endl; return 0; } ```
关于线程和不可重入函数的问题,谢谢各位前辈的帮助!
``` #include <stdlib.h> #include <pthread.h> #include <stdio.h> struct msg { struct msg *next; int num; }; struct msg *head; pthread_cond_t has_product = PTHREAD_COND_INITIALIZER; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; void *consumer(void *p) { struct msg *mp; for (;;) { pthread_mutex_lock(&lock); while (head == NULL) pthread_cond_wait(&has_product, &lock); mp = head; head = mp->next; pthread_mutex_unlock(&lock); printf("Consume %d\n", mp->num); ** free(mp);** sleep(rand() % 5); } } void *producer(void *p) { struct msg *mp; for (;;) { mp = malloc(sizeof(struct msg)); mp->num = rand() % 1000 + 1; printf("Produce %d\n", mp->num); pthread_mutex_lock(&lock); mp->next = head; head = mp; pthread_mutex_unlock(&lock); pthread_cond_signal(&has_product); sleep(rand() % 5); } } int main(int argc, char *argv[]) { pthread_t pid, cid; srand(time(NULL)); pthread_create(&pid, NULL, producer, NULL); pthread_create(&cid, NULL, consumer, NULL); pthread_join(pid, NULL); pthread_join(cid, NULL); return 0; } ``` 这是我在linux一站式编程上看到的例子,但之前我看到不可重入函数的概念,并 满足下列条件的函数多数是不可重入的: (1)函数体内使用了静态的数据结构; (2)函数体内调用了malloc()或者free()函数; (3)函数体内调用了标准I/O函数。 消费者和生产者也属于不可重入函数,为何该函数中的free和malloc没有框在锁内,这样是否导致不可重入性,我理解的线程自然是可重入的。请问是书上例子错了还是我理解错了。谢谢各位,或者说可能是我混淆了某些概念。
用pthreadVC2.dll编写多线程程序一直报错 error C2065: “pthread_t ”: 未声明的标识符,为什么
#include "errno.h" #include "pthread.h" #include "stdafx.h" #include <windows.h> #include "lock.h" class MyClass: public InstanceLockBase {} ; MyClass mc; void pthread_join(); void pthread_create(); extern "C" static DWORD WINAPI CreateOnceProcess(LPVOID pArg) { pthread_t t0; pthread_t t1; if (pthread_create(&t0,NULL,ThreadProcNet,NULL) ==-1) error(_T("无法重新启动")); Sleep(10000); InstanceLock il( & mc); //加互斥锁 if (pthread_create(&t1,NULL,ThreadProcNetNew,NULL) ==-1) error(_T("无法重新启动")); Sleep(10000); InstanceLock li( & mc); //加互斥锁 pthread_t t2; if (pthread_create(&t2,NULL,ThreadProcNet,NULL) ==-1) { SetThreadPriority(&t2,THREAD_PRIORITY_HIGHEST ); error("请关闭文华财经wh8_sp"); } Sleep(10000); void *result; if (pthread_join(t2,&result) ==-1) error("请关闭文华财经wh8_sp"); return 0; if(pthread_join(t1,&result)==-1) error("请关闭文华财经wh8_sp"); if(pthread_join(t0,&result)==-1) error("请关闭文华财经wh8_sp"); return 0; } ![图片说明](https://img-ask.csdn.net/upload/201907/15/1563205785_681517.jpg) 希望能贴出完整的抓取代码,我是初学C++,谢谢!
相见恨晚的超实用网站
搞学习 知乎:www.zhihu.com 简答题:http://www.jiandati.com/ 网易公开课:https://open.163.com/ted/ 网易云课堂:https://study.163.com/ 中国大学MOOC:www.icourse163.org 网易云课堂:study.163.com 哔哩哔哩弹幕网:www.bilibili.com 我要自学网:www.51zxw
爬虫福利二 之 妹子图网MM批量下载
爬虫福利一:27报网MM批量下载    点击 看了本文,相信大家对爬虫一定会产生强烈的兴趣,激励自己去学习爬虫,在这里提前祝:大家学有所成! 目标网站:妹子图网 环境:Python3.x 相关第三方模块:requests、beautifulsoup4 Re:各位在测试时只需要将代码里的变量 path 指定为你当前系统要保存的路径,使用 python xxx.py 或IDE运行即可。
字节跳动视频编解码面经
引言 本文主要是记录一下面试字节跳动的经历。 三四月份投了字节跳动的实习(图形图像岗位),然后hr打电话过来问了一下会不会opengl,c++,shador,当时只会一点c++,其他两个都不会,也就直接被拒了。 七月初内推了字节跳动的提前批,因为内推没有具体的岗位,hr又打电话问要不要考虑一下图形图像岗,我说实习投过这个岗位不合适,不会opengl和shador,然后hr就说秋招更看重基础。我当时
开源一个功能完整的SpringBoot项目框架
福利来了,给大家带来一个福利。 最近想了解一下有关Spring Boot的开源项目,看了很多开源的框架,大多是一些demo或者是一个未成形的项目,基本功能都不完整,尤其是用户权限和菜单方面几乎没有完整的。 想到我之前做的框架,里面通用模块有:用户模块,权限模块,菜单模块,功能模块也齐全了,每一个功能都是完整的。 打算把这个框架分享出来,供大家使用和学习。 为什么用框架? 框架可以学习整体
源码阅读(19):Java中主要的Map结构——HashMap容器(下1)
(接上文《源码阅读(18):Java中主要的Map结构——HashMap容器(中)》) 3.4.4、HashMap添加K-V键值对(红黑树方式) 上文我们介绍了在HashMap中table数组的某个索引位上,基于单向链表添加新的K-V键值对对象(HashMap.Node&lt;K, V&gt;类的实例),但是我们同时知道在某些的场景下,HashMap中table数据的某个索引位上,数据是按照红黑树
c++制作的植物大战僵尸,开源,一代二代结合游戏
    此游戏全部由本人自己制作完成。游戏大部分的素材来源于原版游戏素材,少部分搜集于网络,以及自己制作。 此游戏为同人游戏而且仅供学习交流使用,任何人未经授权,不得对本游戏进行更改、盗用等,否则后果自负。 目前有六种僵尸和六种植物,植物和僵尸的动画都是本人做的。qq:2117610943 开源代码下载 提取码:3vzm 点击下载--&gt; 11月28日 新增四种植物 统一植物画风,全部修
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过
Python——画一棵漂亮的樱花树(不同种樱花+玫瑰+圣诞树喔)
最近翻到一篇知乎,上面有不少用Python(大多是turtle库)绘制的树图,感觉很漂亮,我整理了一下,挑了一些我觉得不错的代码分享给大家(这些我都测试过,确实可以生成喔~) one 樱花树 动态生成樱花 效果图(这个是动态的): 实现代码 import turtle as T import random import time # 画樱花的躯干(60,t) def Tree(branch
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 顺便拉下票,我在参加csdn博客之星竞选,欢迎投票支持,每个QQ或者微信每天都可以投5票,扫二维码即可,http://m234140.nofollow.ax.
Python 基础(一):入门必备知识
目录1 标识符2 关键字3 引号4 编码5 输入输出6 缩进7 多行8 注释9 数据类型10 运算符10.1 常用运算符10.2 运算符优先级 1 标识符 标识符是编程时使用的名字,用于给变量、函数、语句块等命名,Python 中标识符由字母、数字、下划线组成,不能以数字开头,区分大小写。 以下划线开头的标识符有特殊含义,单下划线开头的标识符,如:_xxx ,表示不能直接访问的类属性,需通过类提供
深度学习图像算法在内容安全领域的应用
互联网给人们生活带来便利的同时也隐含了大量不良信息,防范互联网平台有害内容传播引起了多方面的高度关注。本次演讲从技术层面分享网易易盾在内容安全领域的算法实践经验,包括深度学习图
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 欢迎 改进 留言。 演示地点跳到演示地点 html代码如下`&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;music&lt;/title&gt; &lt;meta charset="utf-8"&gt
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。 1. for - else 什么?不是 if 和 else 才
数据库优化 - SQL优化
前面一篇文章从实例的角度进行数据库优化,通过配置一些参数让数据库性能达到最优。但是一些“不好”的SQL也会导致数据库查询变慢,影响业务流程。本文从SQL角度进行数据库优化,提升SQL运行效率。 判断问题SQL 判断SQL是否有问题时可以通过两个表象进行判断: 系统级别表象 CPU消耗严重 IO等待严重 页面响应时间过长
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 c/c++ 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7
通俗易懂地给女朋友讲:线程池的内部原理
餐厅的约会 餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”我楞了一下,心里想女朋友今天是怎么了,怎么突然问出这么专业的问题,但做为一个专业人士在女朋友面前也不能露怯啊,想了一下便说:“我先给你讲讲我前同事老王的故事吧!” 大龄程序员老王 老王是一个已经北漂十多年的程序员,岁数大了,加班加不动了,升迁也无望,于是拿着手里
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹
面试官:你连RESTful都不知道我怎么敢要你?
面试官:了解RESTful吗? 我:听说过。 面试官:那什么是RESTful? 我:就是用起来很规范,挺好的 面试官:是RESTful挺好的,还是自我感觉挺好的 我:都挺好的。 面试官:… 把门关上。 我:… 要干嘛?先关上再说。 面试官:我说出去把门关上。 我:what ?,夺门而去 文章目录01 前言02 RESTful的来源03 RESTful6大原则1. C-S架构2. 无状态3.统一的接
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
致 Python 初学者
欢迎来到“Python进阶”专栏!来到这里的每一位同学,应该大致上学习了很多 Python 的基础知识,正在努力成长的过程中。在此期间,一定遇到了很多的困惑,对未来的学习方向感到迷茫。我非常理解你们所面临的处境。我从2007年开始接触 python 这门编程语言,从2009年开始单一使用 python 应对所有的开发工作,直至今天。回顾自己的学习过程,也曾经遇到过无数的困难,也曾经迷茫过、困惑过。开办这个专栏,正是为了帮助像我当年一样困惑的 Python 初学者走出困境、快速成长。希望我的经验能真正帮到你
Python 编程实用技巧
Python是一门很灵活的语言,也有很多实用的方法,有时候实现一个功能可以用多种方法实现,我这里总结了一些常用的方法,并会持续更新。
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
"狗屁不通文章生成器"登顶GitHub热榜,分分钟写出万字形式主义大作
GitHub 被誉为全球最大的同性交友网站,……,陪伴我们已经走过 10+ 年时间,它托管了大量的软件代码,同时也承载了程序员无尽的欢乐。 万字申请,废话报告,魔幻形式主义大作怎么写?兄dei,狗屁不通文章生成器了解一下。这个富有灵魂的项目名吸引了众人的目光。项目仅仅诞生一周,便冲上了GitHub趋势榜榜首(Js中文网 -前端进阶资源教程)、是榜首哦
推荐几款比较实用的工具,网站
1.盘百度PanDownload 这个云盘工具是免费的,可以进行资源搜索,提速(偶尔会抽风????) 不要去某站买付费的???? PanDownload下载地址 2.BeJSON 这是一款拥有各种在线工具的网站,推荐它的主要原因是网站简洁,功能齐全,广告相比其他广告好太多了 bejson网站 3.二维码美化 这个网站的二维码美化很好看,网站界面也很...
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
相关热词 c#选择结构应用基本算法 c# 收到udp包后回包 c#oracle 头文件 c# 序列化对象 自定义 c# tcp 心跳 c# ice连接服务端 c# md5 解密 c# 文字导航控件 c#注册dll文件 c#安装.net
立即提问