- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 pthreads 创建了一个 C 程序,在我添加任何类型的 sleep 函数之前它都能完美运行,无论是 sleep() 还是 usleep() 或 nanosleep()。
是的,在你问之前,我知道 usleep() 已被弃用,我已经从另一个 stackoferflow 帖子中复制了 nanosleep() 的正确用法,但即使 sleep() 也不起作用。
代码(临界区位于“客户端”线程中,第 99 行):
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <time.h>
#define LIBERA 0
#define OCCUPATA 1
#define TUTTE_SEDIE_OCCUPATE -1
#define OCCUPATA_DA_BARBIERE -2
#define NRCLIENTI 10
#define NRSEDIE 5
pthread_mutex_t poltrona_m, dorme_m, get_sedia_m, occupa_sedia_m;
sem_t sem;
int sedia_taglio = LIBERA, sedia_attesa[NRSEDIE];
void init_attr(void) {
int i;
for (i = 0; i < NRSEDIE; i++)
sedia_attesa[i] = LIBERA;
sem_init(&sem, 0, NRSEDIE);
pthread_mutex_unlock(&poltrona_m);
pthread_mutex_unlock(&dorme_m);
pthread_mutex_unlock(&get_sedia_m);
pthread_mutex_unlock(&occupa_sedia_m);
}
int my_sleep(long millis) {
struct timespec req, rem;
if (millis > 999) {
req.tv_sec = (int) (millis / 1000);
req.tv_nsec = (millis - ((long) req.tv_sec * 1000)) * 1000000;
} else {
req.tv_sec = 0;
req.tv_nsec = millis * 1000000;
}
return nanosleep(&req, &rem);
}
int get_sedia_libera() {
pthread_mutex_lock(&get_sedia_m);
int i;
for (i = 0; i < NRSEDIE; i++)
if (sedia_attesa[i] == LIBERA)
return i;
pthread_mutex_unlock(&get_sedia_m);
return TUTTE_SEDIE_OCCUPATE;
}
int occupa_sedia(int index) {
pthread_mutex_lock(&occupa_sedia_m);
int result;
if(sedia_attesa[index] == LIBERA){
sedia_attesa[index] = OCCUPATA;
result = 1;
}else
result = 0;
pthread_mutex_unlock(&occupa_sedia_m);
return result;
}
void libera_sedia(int index) {
sedia_attesa[index] = LIBERA;
}
void *barbiere(void *arg) {
while (1)
{
if (sedia_taglio == OCCUPATA) {
pthread_mutex_lock(&poltrona_m);
sedia_taglio = LIBERA;
pthread_mutex_unlock(&poltrona_m);
}
if (get_sedia_libera() == 0 && sedia_taglio == LIBERA) {
sedia_taglio = OCCUPATA_DA_BARBIERE;
pthread_mutex_lock(&dorme_m);
printf("[BARBIERE] il barbiere dorme...\n");
}
}
}
void *cliente(void *arg) {
int n;
int *id = (void *)arg;
while (1)
{
if (sedia_taglio == LIBERA || sedia_taglio == OCCUPATA_DA_BARBIERE) {
pthread_mutex_lock(&poltrona_m);
pthread_mutex_unlock(&dorme_m);
sedia_taglio = OCCUPATA;
printf("----[CLIENTE] Cliente \"%d\" si taglia i capelli!\n", *id);
my_sleep(2000);
sedia_taglio = LIBERA;
pthread_mutex_unlock(&poltrona_m);
pthread_exit(0);
} else {
n = get_sedia_libera();
if(n == TUTTE_SEDIE_OCCUPATE) {
fprintf(stderr, "----[CLIENTE] No posti liberi cliente \"%d\" lascia il negozio\n", *id);
pthread_exit((void *)1);
} else {
sem_wait(&sem);
n = get_sedia_libera();
if(occupa_sedia(n) && n != TUTTE_SEDIE_OCCUPATE){
printf("----[CLIENTE] Cliente \"%d\" aspetta su sedia #%d\n", *id, n+1);
while (sedia_taglio != LIBERA);
sem_post(&sem);
libera_sedia(n);
} else {
sem_post(&sem);
fprintf(stderr, "----[CLIENTE] No posti liberi cliente \"%d\" lascia il negozio\n", *id);
pthread_exit(0);
}
}
}
}
}
int main(int argc, char **argv) {
init_attr();
int i, id[NRCLIENTI];
pthread_t barbiere_thread, cliente_thread[NRCLIENTI];
if(pthread_create(&barbiere_thread, NULL, barbiere, NULL ) < 0){
fprintf(stderr, "[MAIN] Error creating \"barber\" thread!\n");
exit(EXIT_FAILURE);
}
printf("[MAIN] thread \"barber\" creato\n\n");
for (i = 0; i < NRCLIENTI; ++i) {
id[i] = i+1;
printf("[MAIN] thread \"client %d\" creato\n", id[i]);
if(pthread_create(&cliente_thread[i], NULL, cliente, &id[i]) < 0){
fprintf(stderr, "[MAIN] Error creating \"client %d\" thread!\n", id[i]);
exit(EXIT_FAILURE);
}
}
for (i = 0; i < NRCLIENTI; i++){
pthread_join(cliente_thread[i], NULL);
printf("[MAIN]Joined client \"%d\"\n", i+1);
}
//il programma non finira mai se sta ad aspettare un thread con un ciclo while(1) al suo interno
//senza condizioni di break!!!
pthread_join(barbiere_thread, NULL);
return 0;
}
[MAIN] thread "client 1" creato
[BARBIERE] il barbiere dorme...
hey
[MAIN] thread "client 2" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "1" si taglia i capelli!
[MAIN] thread "client 3" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "2" si taglia i capelli!
[MAIN] thread "client 4" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "3" si taglia i capelli!
[MAIN] thread "client 5" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "4" si taglia i capelli!
[MAIN] thread "client 6" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "5" si taglia i capelli!
[MAIN] thread "client 7" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "6" si taglia i capelli!
[MAIN] thread "client 8" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "7" si taglia i capelli!
[MAIN] thread "client 9" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "8" si taglia i capelli!
[MAIN] thread "client 10" creato
----<<<<<<<<<<<<[CLIENTE] Cliente "9" si taglia i capelli!
----<<<<<<<<<<<<[CLIENTE] Cliente "10" si taglia i capelli!
[MAIN]Joined client "1"
[MAIN]Joined client "2"
[MAIN]Joined client "3"
[MAIN]Joined client "4"
[MAIN]Joined client "5"
[MAIN]Joined client "6"
[MAIN]Joined client "7"
[MAIN]Joined client "8"
[MAIN]Joined client "9"
[MAIN]Joined client "10"
[MAIN] thread "client 1" creato
[BARBIERE] il barbiere dorme...
[MAIN] thread "client 2" creato
----[CLIENTE] Cliente "1" si taglia i capelli!
[MAIN] thread "client 3" creato
[MAIN] thread "client 4" creato
[MAIN] thread "client 5" creato
[MAIN] thread "client 6" creato
[MAIN] thread "client 7" creato
[MAIN] thread "client 8" creato
[MAIN] thread "client 9" creato
[MAIN] thread "client 10" creato
[MAIN]Joined client "1"
最佳答案
您创建死锁是因为线程之间存在竞争条件。
搬家 pthread_mutex_t poltrona_m, dorme_m, get_sedia_m, occupa_sedia_m;
在全局范围内,添加这些冗长的函数:
const char * whoIam()
{
if (pthread_self() == barbiere_thread)
return "barbiere";
for (int i = 0; i != NRCLIENTI; ++i) {
if (pthread_self() == cliente_thread[i]) {
static char str[NRCLIENTI][64];
sprintf(str[i], "cliente %d", i+1); /* done several times but whatever */
return str[i];
}
}
return "unknown-thread";
}
const char * whoIsMutex(pthread_mutex_t * m)
{
if (m == &poltrona_m)
return "poltrona";
if (m == &dorme_m)
return "dorme";
if (m == &get_sedia_m)
return "sedia";
if (m == &occupa_sedia_m)
return "occupa_sedia";
return "unknown-mutex";
}
void my_pthread_mutex_unlock(pthread_mutex_t * m)
{
printf("%s unlock %s\n", whoIam(), whoIsMutex(m));
pthread_mutex_unlock(m);
}
void my_pthread_mutex_lock(pthread_mutex_t * m)
{
printf("%s want to lock %s\n", whoIam(), whoIsMutex(m));
pthread_mutex_lock(m);
printf("%s locked %s\n", whoIam(), whoIsMutex(m));
}
my_pthread_mutex_lock
和
my_pthread_mutex_unlock
出
init_attr
执行可以是:
[MAIN] thread "barber" creato
[MAIN] thread "client 1" creato
barbiere want to lock sedia
barbiere locked sedia
barbiere want to lock dorme
barbiere locked dorme
cliente 1 want to lock poltrona
cliente 1 locked poltrona
cliente 1 unlock dorme <<< does nothing because does not have it
----[CLIENTE] Cliente "1" si taglia i capelli!
[BARBIERE] il barbiere dorme...
barbiere want to lock poltrona <<< wait while get by client 1
[MAIN] thread "client 2" creato
[MAIN] thread "client 3" creato
[MAIN] thread "client 4" creato
cliente 2 want to lock sedia
[MAIN] thread "client 5" creato
cliente 4 want to lock sedia
[MAIN] thread "client 6" creato
cliente 3 want to lock sedia
[MAIN] thread "client 7" creato
cliente 6 want to lock sedia
cliente 5 want to lock sedia
[MAIN] thread "client 8" creato
cliente 7 want to lock sedia
[MAIN] thread "client 9" creato
cliente 8 want to lock sedia
[MAIN] thread "client 10" creato
cliente 9 want to lock sedia
cliente 10 want to lock sedia
cliente 1 unlock poltrona <<< finally unlock poltrona
barbiere locked poltrona <<< wake up
barbiere unlock poltrona
barbiere want to lock sedia <<< dead because get by itself
[MAIN]Joined client "1"
my_sleep(1000);
之前
sedia_taglio = occupata;
在客户中有助于没有竞争条件:
bruno@bruno-XPS-8300:/tmp$ gcc -Wall t.c -lpthread
bruno@bruno-XPS-8300:/tmp$ ./a.out
[MAIN] thread "barber" creato
[MAIN] thread "client 1" creato
[BARBIERE] il barbiere dorme...
[MAIN] thread "client 2" creato
[MAIN] thread "client 3" creato
[MAIN] thread "client 4" creato
[MAIN] thread "client 5" creato
[MAIN] thread "client 6" creato
[MAIN] thread "client 7" creato
[MAIN] thread "client 8" creato
[MAIN] thread "client 9" creato
[MAIN] thread "client 10" creato
----[CLIENTE] Cliente "1" si taglia i capelli!
[MAIN]Joined client "1"
----[CLIENTE] Cliente "3" si taglia i capelli!
----[CLIENTE] Cliente "2" si taglia i capelli!
[MAIN]Joined client "2"
[MAIN]Joined client "3"
----[CLIENTE] Cliente "4" si taglia i capelli!
[MAIN]Joined client "4"
----[CLIENTE] Cliente "5" si taglia i capelli!
[MAIN]Joined client "5"
----[CLIENTE] Cliente "6" si taglia i capelli!
[MAIN]Joined client "6"
----[CLIENTE] Cliente "7" si taglia i capelli!
[MAIN]Joined client "7"
----[CLIENTE] Cliente "8" si taglia i capelli!
[MAIN]Joined client "8"
----[CLIENTE] Cliente "9" si taglia i capelli!
[MAIN]Joined client "9"
----[CLIENTE] Cliente "10" si taglia i capelli!
[MAIN]Joined client "10"
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#define NCLIENT 10
#define NSEAT 5 /* seats to wait */
unsigned int Seed;
int Nseat = NSEAT + 1; /* add 1 to have seat to cut hairs */
int CurrentTicket = 0;
int LastTicket = 0;
pthread_mutex_t Mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t NextClient = PTHREAD_COND_INITIALIZER;
pthread_cond_t HelloBarber = PTHREAD_COND_INITIALIZER;
/* return -1 is no seat, else ticket number */
int getTicket()
{
int result;
pthread_mutex_lock(&Mutex);
if (Nseat == 0)
result = -1;
else {
Nseat -= 1;
result = ++LastTicket;
}
pthread_mutex_unlock(&Mutex);
return result;
}
void randomSleep(int max)
{
int millis = rand_r(&Seed) % (max * 1000);
struct timespec req, rem;
if (millis > 999) {
req.tv_sec = (int) (millis / 1000);
req.tv_nsec = (millis - ((long) req.tv_sec * 1000)) * 1000000;
} else {
req.tv_sec = 0;
req.tv_nsec = millis * 1000000;
}
nanosleep(&req, &rem);
}
void * client(void * arg)
{
int id = *((int *) arg);
int ticket;
int justArrived = 1;
while (randomSleep(5), ((ticket = getTicket()) == -1)) {
pthread_mutex_lock(&Mutex);
printf("----[CLIENT] Client \"%d\" no place, will try again later\n", id);
pthread_mutex_unlock(&Mutex);
}
pthread_mutex_lock(&Mutex);
printf("----[CLIENT] Client \"%d\" got ticket number %d\n", id, ticket);
while (ticket != CurrentTicket) {
printf("----[CLIENT] Client \"%d\" not my turn\n", id);
if (justArrived) {
justArrived = 0;
printf("----[CLIENT] Client \"%d\" seat to wait\n", id);
}
pthread_cond_wait(&NextClient, &Mutex);
}
printf("----[CLIENT] Client \"%d\" my turn\n", id);
if (justArrived)
printf("----[CLIENT] Client \"%d\" do not need to seat to wait\n", id);
pthread_cond_signal(&HelloBarber);
pthread_cond_wait(&NextClient, &Mutex);
printf("----[CLIENT] Client \"%d\" done for me\n", id);
pthread_mutex_unlock(&Mutex);
return NULL;
}
void * barber(void * dummy)
{
pthread_mutex_lock(&Mutex);
puts("[BARBER] ready");
CurrentTicket = 1;
pthread_cond_signal(&NextClient);
for (;;) {
printf("[BARBER] screen indicates ticket %d\n", CurrentTicket);
int sleep = (Nseat == NSEAT + 1);
if (sleep)
puts("[BARBER] no client, time to sleep");
pthread_cond_wait(&HelloBarber, &Mutex); /* in all cases to be sure next client had time to see his turn */
if (sleep)
puts("[BARBER] woken up by a client");
puts("[BARBER] cutting hairs");
pthread_mutex_unlock(&Mutex);
randomSleep(3); /* time to cut hairs of current client */
pthread_mutex_lock(&Mutex);
puts("[BARBER] haircut done");
Nseat += 1;
CurrentTicket += 1;
pthread_cond_broadcast(&NextClient);
}
}
int main()
{
pthread_t barber_thread, client_thread[NCLIENT];
int i, id[NCLIENT];
Seed = time(NULL);
if (pthread_create(&barber_thread, NULL, barber, NULL ) < 0){
fprintf(stderr, "[MAIN] Error creating \"barber\" thread!\n");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&Mutex);
if (CurrentTicket == 0) {
/* wait barber ready */
pthread_cond_wait(&NextClient, &Mutex);
}
pthread_mutex_unlock(&Mutex);
for (i = 0; i < NCLIENT; ++i) {
id[i] = i+1;
if (pthread_create(&client_thread[i], NULL, client, &id[i]) < 0) {
fprintf(stderr, "[MAIN] Error creating \"client %d\" thread!\n", id[i]);
exit(EXIT_FAILURE);
}
}
for (i = 0; i < NCLIENT; i++) {
pthread_join(client_thread[i], NULL);
pthread_mutex_lock(&Mutex);
printf("[MAIN] Joined client \"%d\"\n", i+1);
pthread_mutex_unlock(&Mutex);
}
return 0;
}
pi@raspberrypi:~ $ gcc -Wall barber.c -lpthread
pi@raspberrypi:~ $ ./a.out
[BARBER] ready
[BARBER] screen indicates ticket 1
[BARBER] no client, time to sleep
----[CLIENT] Client "7" got ticket number 1
----[CLIENT] Client "7" my turn
----[CLIENT] Client "7" do not need to seat to wait
[BARBER] woken up by a client
[BARBER] cutting hairs
----[CLIENT] Client "6" got ticket number 2
----[CLIENT] Client "6" not my turn
----[CLIENT] Client "6" seat to wait
----[CLIENT] Client "5" got ticket number 3
----[CLIENT] Client "5" not my turn
----[CLIENT] Client "5" seat to wait
----[CLIENT] Client "1" got ticket number 4
----[CLIENT] Client "1" not my turn
----[CLIENT] Client "1" seat to wait
----[CLIENT] Client "3" got ticket number 5
----[CLIENT] Client "3" not my turn
----[CLIENT] Client "3" seat to wait
[BARBER] haircut done
[BARBER] screen indicates ticket 2
----[CLIENT] Client "7" done for me
----[CLIENT] Client "3" not my turn
----[CLIENT] Client "1" not my turn
----[CLIENT] Client "6" my turn
----[CLIENT] Client "5" not my turn
[BARBER] cutting hairs
----[CLIENT] Client "9" got ticket number 6
----[CLIENT] Client "9" not my turn
----[CLIENT] Client "9" seat to wait
----[CLIENT] Client "8" got ticket number 7
----[CLIENT] Client "8" not my turn
----[CLIENT] Client "8" seat to wait
----[CLIENT] Client "4" no place, will try again later
----[CLIENT] Client "2" no place, will try again later
----[CLIENT] Client "10" no place, will try again later
[BARBER] haircut done
[BARBER] screen indicates ticket 3
----[CLIENT] Client "8" not my turn
----[CLIENT] Client "5" my turn
----[CLIENT] Client "9" not my turn
----[CLIENT] Client "1" not my turn
----[CLIENT] Client "6" done for me
[BARBER] cutting hairs
----[CLIENT] Client "3" not my turn
[BARBER] haircut done
[BARBER] screen indicates ticket 4
----[CLIENT] Client "9" not my turn
----[CLIENT] Client "1" my turn
----[CLIENT] Client "3" not my turn
----[CLIENT] Client "5" done for me
----[CLIENT] Client "8" not my turn
[BARBER] cutting hairs
[BARBER] haircut done
[BARBER] screen indicates ticket 5
----[CLIENT] Client "8" not my turn
----[CLIENT] Client "1" done for me
[MAIN] Joined client "1"
----[CLIENT] Client "9" not my turn
----[CLIENT] Client "3" my turn
[BARBER] cutting hairs
----[CLIENT] Client "10" got ticket number 8
----[CLIENT] Client "10" not my turn
----[CLIENT] Client "10" seat to wait
----[CLIENT] Client "2" got ticket number 9
----[CLIENT] Client "2" not my turn
----[CLIENT] Client "2" seat to wait
----[CLIENT] Client "4" got ticket number 10
----[CLIENT] Client "4" not my turn
----[CLIENT] Client "4" seat to wait
[BARBER] haircut done
[BARBER] screen indicates ticket 6
----[CLIENT] Client "2" not my turn
----[CLIENT] Client "3" done for me
----[CLIENT] Client "4" not my turn
----[CLIENT] Client "8" not my turn
----[CLIENT] Client "9" my turn
----[CLIENT] Client "10" not my turn
[BARBER] cutting hairs
[BARBER] haircut done
[BARBER] screen indicates ticket 7
----[CLIENT] Client "2" not my turn
----[CLIENT] Client "9" done for me
----[CLIENT] Client "4" not my turn
----[CLIENT] Client "8" my turn
[BARBER] cutting hairs
----[CLIENT] Client "10" not my turn
[BARBER] haircut done
[BARBER] screen indicates ticket 8
----[CLIENT] Client "10" my turn
[BARBER] cutting hairs
----[CLIENT] Client "2" not my turn
----[CLIENT] Client "8" done for me
----[CLIENT] Client "4" not my turn
[BARBER] haircut done
[BARBER] screen indicates ticket 9
----[CLIENT] Client "10" done for me
----[CLIENT] Client "2" my turn
----[CLIENT] Client "4" not my turn
[BARBER] cutting hairs
[BARBER] haircut done
[BARBER] screen indicates ticket 10
----[CLIENT] Client "2" done for me
----[CLIENT] Client "4" my turn
[BARBER] cutting hairs
[MAIN] Joined client "2"
[MAIN] Joined client "3"
[BARBER] haircut done
[BARBER] screen indicates ticket 11
[BARBER] no client, time to sleep
----[CLIENT] Client "4" done for me
[MAIN] Joined client "4"
[MAIN] Joined client "5"
[MAIN] Joined client "6"
[MAIN] Joined client "7"
[MAIN] Joined client "8"
[MAIN] Joined client "9"
[MAIN] Joined client "10"
pi@raspberrypi:~ $
关于c - RESOLVED sleep 方法将程序线程卡在 C pthreads 中(竞争条件,它与 sleep 无关),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62118182/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!